//
//  ForgetPWVC.m
//  jinwang
//
//  Created by jzb on 2018/3/9.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "ForgetPWVC.h"

#define KTime   60//设置重新发送的时间  自己可以改

@interface ForgetPWVC ()
{
    NSTimer *_myTimer;//定时器
}
@property (weak, nonatomic) IBOutlet UITextField *PhoneTF;
@property (weak, nonatomic) IBOutlet UITextField *CodeTF;
@property (weak, nonatomic) IBOutlet UITextField *PassWordTF;
@property (nonatomic,assign) NSInteger timer;

@end

@implementation ForgetPWVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"忘记密码";
    _timer = KTime;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([textField isEqual:self.PhoneTF]) {
        NSUInteger length = textField.text.length + string.length - range.length;
        if (length <= 11) {
            return YES;
        }else{
            [self.PhoneTF shakeWithShakeDirection:ShakingDirectionHorizontal];
            return NO;
        }
    }else if ([textField isEqual:self.CodeTF]) {
        NSUInteger length = textField.text.length + string.length - range.length;
        if (length <= 6) {
            return YES;
        }else{
            [self.CodeTF shakeWithShakeDirection:ShakingDirectionHorizontal];
            return NO;
        }
    }else if ([textField isEqual:self.PassWordTF]) {
        NSUInteger length = textField.text.length + string.length - range.length;
        if (length <= 6) {
            return YES;
        }else{
            [self.PassWordTF shakeWithShakeDirection:ShakingDirectionHorizontal];
            return NO;
        }
    }else{
        return YES;
    }
}
- (IBAction)CodeBtnOnclick:(UIButton *)sender {
    
    if (kStringIsEmpty(self.PhoneTF.text)) {
        [self showBottomMessag:@"请输入手机号"];
        return;
    }
    else if (![NSString IsPhoneNumber:self.PhoneTF.text]) {
        [self showBottomMessag:@"请输入正确的手机号"];
        return;
    }
    else{
        [self getCode:self.PhoneTF.text currentBtn:sender];
    }
}

#pragma mark -- 获取验证码
- (void)getCode:(NSString *)text currentBtn:(UIButton *)currentBtn
{
    [[DDNetworkManagerDate makeUrl_sendsms_with_user_phone:text type:@"2"] post_RequestFinshSuccess:^(id responseObject) {
        NSLog(@"====>>>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            NSDictionary *dic = responseObject;
            
            NSLog(@"====>>>%@",dic);
            currentBtn.enabled = NO;
            [currentBtn setTitle:[NSString stringWithFormat:@"剩余%zis", _timer] forState:UIControlStateNormal];
            _myTimer = [NSTimer scheduledTimerWithTimeInterval:1 block:^(NSTimer * _Nonnull timer) {
                [currentBtn setTitle:[NSString stringWithFormat:@"剩余%zis", _timer] forState:UIControlStateNormal];
                if (_timer == 0) {
                    [currentBtn setTitle:@"重新获取" forState:UIControlStateNormal];
                    currentBtn.enabled = YES;
                    [_myTimer invalidate];
                    _myTimer = nil;
                    _timer = KTime;
                }else{
                    _timer --;
                }
            } repeats:YES];
        }
    } failure:^(id errorObject) {
    }];
}
- (IBAction)QueRenBtnOnclick:(UIButton *)sender {
    
    //确认
    if (kStringIsEmpty(self.PhoneTF.text)) {
        [self showBottomMessag:@"请输入手机号"];
        return;
    }else if (![NSString IsPhoneNumber:self.PhoneTF.text]) {
        [self showBottomMessag:@"请输入正确的手机号"];
        return;
    }else if (self.CodeTF.text.length < 6) {
        [self showCenterMessage:@"验证码长度为六位"];
        return;
    }else if (self.PassWordTF.text.length < 6) {
        [self showCenterMessage:@"密码长度为六位"];
        return;
    }
    
    [[DDNetworkManagerDate makeUrl_forgetpwd_with_user_phone:self.PhoneTF.text user_password:self.PassWordTF.text code:self.CodeTF.text] post_RequestFinshSuccess:^(id responseObject) {
        NSLog(@"-=-=-=-=>>>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            
            [kUserDefaults setObject:self.PhoneTF.text forKey:@"user_phone"];
            [kUserDefaults setObject:self.PassWordTF.text forKey:@"user_pwd"];
            
            [self goBack];
            
        }
    } failure:^(id errorObject) {
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
